﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using InterfacesInstanceClustering;
using DTO;

namespace MyClusterTool
{
    public partial class ClutoClusteringPlugin : IClusteringUC, IClustering
    {
        public ClutoClusteringPlugin()
        {
            InitializeComponent();
        }

        private IInstanceClusteringHost pluginHost = null;
        public IInstanceClusteringHost PluginHost
        {
            get
            {
                return pluginHost;
            }
            set
            {
                this.pluginHost = value;
            }
        }

        private string pluginName = "K-Means Cluto Clustering";
        public string PluginName
        {
            get
            {
                return this.pluginName;
            }
        }

        private string pluginDescription = "Cluster high demensional vectors using K-Means.";

        public string PluginDescription
        {
            get { return pluginDescription; }
            set { pluginDescription = value; }
        }

        public IClusteringUC PluginUserControl
        {
            get
            {
                return this;
            }
        }

        public void Initialize()
        {
        }

        public void PluginDispose()
        {
        }

        MyClusterToolPluginForm myForm = new MyClusterToolPluginForm();        
        /// <summary>
        /// This method run the clustering exec file to cluster all vectors.
        /// </summary>
        /// <param name="execPath">The clustering exec file path</param>
        /// <param name="vectorFilePath">The path of the vectors file.</param>
        /// <param name="numberOfCluster">The desired number of clusters.</param>
        private void RunCluto(string execPath, string vectorFilePath, uint numberOfCluster)
        {
            try
            {
                System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(execPath);
                psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                psi.CreateNoWindow = false;
                psi.UseShellExecute = false;
                psi.Arguments = vectorFilePath + " " + numberOfCluster;
                System.Diagnostics.Process curProcess;
                curProcess = System.Diagnostics.Process.Start(psi);
                curProcess.WaitForExit();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        

        /// <summary>
        /// Make the input file for cluto clustering tool
        /// </summary>
        private void MakeClutoInput()
        {
            try
            {
                List<Vector> pagesVectorList = Vector.FromVectorFile(this.PluginUserControl.InputVectorFile);
                FileStream fs = new FileStream(Directory.GetCurrentDirectory() + @"\" + "clutoInput" + ".mat", FileMode.Create, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);

                sw.WriteLine(pagesVectorList.Count + " " + pagesVectorList[0].Value.Count);

                foreach (Vector i in pagesVectorList)
                {
                    int j = 0;
                    for (; j < i.Value.Count - 1; j++)
                    {
                        sw.Write(i.Value[j] + " ");
                    }
                    sw.Write(i.Value[j]);
                    sw.WriteLine();
                }

                sw.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }            
        }

        public void Cluster()
        {
            try
            {
                MakeClutoInput();
                string inputClutoFile = Directory.GetCurrentDirectory() + @"\" + "clutoInput" + ".mat";
                RunCluto(this.myForm.ExeClusterFilePath, inputClutoFile, this.myForm.NumOfCluster);
                File.Move(Directory.GetCurrentDirectory() + @"\" + "clutoInput.mat.clustering." + this.myForm.NumOfCluster.ToString()
                    , this.OutputClusterPath + @"\" + "clutoOutput.mat.clustering." + this.myForm.NumOfCluster.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void button_OtherPara_Click(object sender, EventArgs e)
        {
            if (this.myForm.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("Parameter Set!");
            }
            else
            {
                MessageBox.Show("Parameter Not Set! Be carefull!");
            }
        }
    }
}
